using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Text;
using Iivil.Infrastructure.Configuration;
using Iivil.Infrastructure.Logging;

namespace Iivil.Infrastructure.Logging.Providers {
	/// <summary>
	/// Summary description for FileLogProvider.
	/// </summary>
	public class FileLogProvider : ILogWriter {
        //private FileStream _instance;
	    private static readonly string _logDirectory = ConfigurationProvider.GetString("LogDirectory");
	    public FileLogProvider() {
            if (!Directory.Exists(_logDirectory))
                Directory.CreateDirectory(_logDirectory);
		}

	    public void WriteLogEntry(Severity severity, string category, string formatMessage, params object[] args) {
            WriteLogEntry(severity, category, String.Format(formatMessage, args));
	    }

	    public void WriteLogEntry(Severity severity, string category, string message) {
            FileStream _instance = new FileStream(Path.Combine(_logDirectory,  DateTime.Now.Date.ToString("yyyyMMdd") + ".log") , FileMode.Append, FileAccess.Write, FileShare.Read);
            using (StreamWriter sw = new StreamWriter(_instance)) {
                StringBuilder sb = new StringBuilder(DateTime.Now.ToShortTimeString() + '\t');
                sb.Append(severity.ToString());
                sb.Append('\t');
                sb.Append(category);
                sb.Append('\t');
                sb.Append(message);
                sw.WriteLine(sb.ToString());
                sw.Close();
            }
            _instance.Close();
        }

	    public void Dispose() {
	    }
	}
}
